﻿// ***********************************************************************
// Assembly         : INNOVACALL.Framework.Common
// Author           : LJO+01 (Laurent Jordi)
// Created          : 06-05-2013
// Last Modified By : LJO+01 (Laurent Jordi)
// Last Modified On : 06-05-2013
// ***********************************************************************
// <copyright file="DebugFilterProvider.cs" company="Copyright © Innovacall 2011">
//     Copyright (c) Copyright © Innovacall 2011. All rights reserved.
// </copyright>
// <summary></summary>
// ***********************************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;

namespace INNOVACALL.Framework.Common.MVCHelpers
{
    /// <summary>
    /// Class DebugFilterProvider
    /// </summary>
    public class DebugFilterProvider : IFilterProvider
    {
        /// <summary>
        /// The _conditions
        /// </summary>
        private readonly List<Func<ControllerContext, object>> _conditions = new List<Func<ControllerContext, object>>();
        /// <summary>
        /// Adds the specified condition.
        /// </summary>
        /// <param name="condition">The condition.</param>
        public void Add(Func<ControllerContext, object> condition)
        {
            _conditions.Add(condition);
        }

        /// <summary>
        /// Retourne un énumérateur qui contient toutes les instances <see cref="T:System.Web.Mvc.IFilterProvider" /> du localisateur de service.
        /// </summary>
        /// <param name="controllerContext">Contexte du contrôleur.</param>
        /// <param name="actionDescriptor">Descripteur de l'action.</param>
        /// <returns>Énumérateur qui contient toutes les instances <see cref="T:System.Web.Mvc.IFilterProvider" /> du localisateur de service.</returns>
        public IEnumerable<Filter> GetFilters(ControllerContext controllerContext, ActionDescriptor actionDescriptor)
        {
            return from condition in _conditions
                   select condition(controllerContext) into filter
                   where filter != null
                   select new Filter(filter, FilterScope.Global, null);
        }
    }
}
